public with sharing class BatchAddStudentsController {

    @AuraEnabled
    public static Student__c[] fetchStudentsByTeacherId(String teacherId) {
        System.debug('参数：teacherId -> ' + teacherId);
        if(teacherId == null || String.isBlank(teacherId)) {
            return new Student__c[0];
        }
        Student__c[] resultList = [SELECT Id, Name, Student_Name__c, Sex__c, School_Fee__c FROM Student__c WHERE Teacher__c = :teacherId];
        System.debug(resultList);
        return resultList;
    }

    @AuraEnabled
    public static string addStudents2Teacher(Student__c[] stuList, String tid){
        if(stuList == null || stuList.size() < 1) {
            throw new AuraHandledException('请选择要导入的学生！');
        }
        if(tid == null || ''.equals(tid.trim())) {
            throw new AuraHandledException('无法获取教师的recordId!');
        }
        try {
            for (Student__c stu : stuList) {
                stu.Id = null;
                stu.Teacher__c = tid;
            }
            upsert stuList;
            return '成功导入学生'+stuList.size()+'个!';
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }


}